<?php

namespace Modules\DataCheck\Http\Controllers;

use Illuminate\Http\Request;
use Modules\DataCheck\Enums\TaskSourceFileEnum;
use Modules\DataCheck\Exceptions\ParamsException;
use Modules\DataCheck\Models\DcTaskSourceFile;

class SourceFileController extends BaseController
{
    public function __construct(protected readonly DcTaskSourceFile $model)
    {
    }

    /**
     * 原始文化详情
     * @param Request $request
     * @return array
     */
    public function detail(Request $request)
    {
        $id = $request->query('id');
        if (empty($id)) {
            throw new ParamsException('id 不能为空');
        }

        $sourceFile = $this->model->find($id);
        if (empty($sourceFile)) {
            return [];
        }

        $remark = '';
        if (TaskSourceFileEnum::FAILED->equal($sourceFile->file_state)) {
            $remark = json_decode($sourceFile->remark,  true);
            $remark = $remark['msg'] ?? '';
        }

        return [
            'id' => $sourceFile->id,
            'file_path' => $sourceFile->file_path,
            'file_state' => $sourceFile->file_state,
            'remark' => $remark,
            'file_state_name' => TaskSourceFileEnum::tryFrom($sourceFile->file_state)->message(),
        ];
    }
}
